package cn.interestingshop.servlet;

import cn.interestingshop.entity.Classify;
import cn.interestingshop.service.ClassifyService;
import cn.interestingshop.service.impl.ClassifyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/ClassifyUpdateServlet")
public class ClassifyUpdateServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.设置编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        //2.获取请求参数
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String type = request.getParameter("type");
        String productCategoryLevel1= request.getParameter("productCategoryLevel1");
        String productCategoryLevel2= request.getParameter("productCategoryLevel2");
        //获取父id
        String parentId=null;
        if(type.equals("1")){
            parentId="0";
        }else if(type.equals("2")){
            parentId=productCategoryLevel1;
        }else if(type.equals("3")){
            parentId=productCategoryLevel2;
        }
        //3.调用service进行业务处理
        ClassifyService service=new ClassifyServiceImpl();
        Classify classify=new Classify();
        classify.setId(Integer.parseInt(id));
        classify.setClassifyName(name);
        classify.setType(Integer.parseInt(type));
        classify.setParentId(Integer.parseInt(parentId));
        int i = service.insert(classify);
        //4.分析返回结果
        if(i>0){
            response.getWriter().print("<script>alert('修改成功');location.href='../back/ClassifySelectAllServlet'</script>");
        }else{
            response.getWriter().print("<script>alert('修改失败');history.go(-1)</script>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
